datacops-cms
Version:
A modern, extensible CMS built with Next.js and Prisma.
128 lines (110 loc) • 4.15 kB
text/typescript
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { modelName } from "@/lib/modelName";
import { PrismaClient } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server";
import { checkAllowed } from "../route";
const prisma = new PrismaClient();
// GET /api/content/[type]/[id]
export async function GET(
req: NextRequest,
{ params }: { params: { type: string; id: string } }
)
{
const { type, id } = await params;
const model = modelName(type);
const allowed = await checkAllowed(type, "GET", req);
if (!allowed) {
return NextResponse.json({ error: "You are not allowed to perform this action" }, { status: 403 });
}
try {
// @ts-ignore
let item = await prisma[model].findUnique({ where: { id } });
if (!item) return NextResponse.json({ error: "Not found" }, { status: 404 });
// Auto-promote scheduled-to-published if due (optional, can remove)
if (
item.status === "Scheduled" &&
item.schedule &&
new Date(item.schedule) <= new Date()
) {
// @ts-ignore
item = await prisma[model].update({
where: { id },
data: { status: "Published", schedule: null },
});
}
return NextResponse.json(item);
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 400 });
}
}
// PUT /api/content/[type]/[id]
export async function PATCH(
req: NextRequest,
{ params }: { params: { type: string; id: string } }
)
{
const { type, id } = await params;
const model = modelName(type);
const allowed = await checkAllowed(type, "PATCH", req);
if (!allowed) {
return NextResponse.json({ error: "You are not allowed to perform this action" }, { status: 403 });
}
try {
const data = await req.json();
// Don't allow updating id, createdAt, updatedAt fields
["id", "createdAt", "updatedAt"].forEach(f =>
{
if (f in data) delete data[f];
});
// Fix schedule field for Prisma
if ("schedule" in data) {
if (!data.schedule) {
data.schedule = null;
} else if (data.status === "Scheduled" && typeof data.schedule === "string") {
if (!data.schedule) {
return NextResponse.json(
{ error: "Schedule date is required for Scheduled status." },
{ status: 400 }
);
}
const scheduleDate = new Date(data.schedule);
if (isNaN(scheduleDate.getTime()) || scheduleDate <= new Date()) {
return NextResponse.json(
{ error: "Schedule date must be a valid future date/time." },
{ status: 400 }
);
}
data.schedule = scheduleDate.toISOString();
}
}
// @ts-ignore
const updated = await prisma[model].update({
where: { id },
data,
});
return NextResponse.json(updated);
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 400 });
}
}
// Optionally: DELETE
export async function DELETE(
req: NextRequest,
{ params }: { params: { type: string; id: string } }
)
{
const { type, id } = await params;
const model = modelName(type);
const allowed = await checkAllowed(type, "DELETE", req);
if (!allowed) {
return NextResponse.json({ error: "You are not allowed to perform this action" }, { status: 403 });
}
try {
// @ts-ignore
const deleted = await prisma[model].delete({ where: { id } });
return NextResponse.json(deleted);
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 400 });
}
}